package com.webemail.sys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.webemail.sys.mapper.SysMessageJunkMapper;
import com.webemail.sys.model.SysMessageJunk;
import com.webemail.sys.service.MessageJunkService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户管理
 */
@Service
public class MessageJunkServiceImpl extends ServiceImpl<SysMessageJunkMapper,SysMessageJunk> implements MessageJunkService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	public SysMessageJunkMapper sysMessageJunkMapper;

	@Override
	public int insert(SysMessageJunk temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "insert" + " param:" + JSON.toJSONString(temp));
		return sysMessageJunkMapper.insert(temp);
	}

	@Override
	public int delete(long id, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "delete" + " param:" + "id:" + id + "userId:"
				+ userId);
		SysMessageJunk temp = new SysMessageJunk();
		temp.setId(id);
		temp.setUserId(userId);
		return sysMessageJunkMapper.deleteById(temp);
	}

	@Override
	public int update(SysMessageJunk temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "update" + " param:" + JSON.toJSONString(temp));
		return sysMessageJunkMapper.updateById(temp);
	}

	@Override
	public SysMessageJunk get(long id, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "get" + " param:" + "id:" + id + "userId:"
				+ userId);
		return sysMessageJunkMapper.selectOne(Wrappers.lambdaQuery(SysMessageJunk.class)
				.eq(SysMessageJunk::getId,id)
				.eq(SysMessageJunk::getUserId,userId));
	}

	@Override
	public List<SysMessageJunk> list(long mailId, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "list" + " param:" + "emailId:" + mailId
				+ "userId:" + userId);
		return sysMessageJunkMapper.selectList(Wrappers.lambdaQuery(SysMessageJunk.class)
				.eq(SysMessageJunk::getMailId,mailId)
				.eq(SysMessageJunk::getUserId,userId));
	}

	@Override
	public int insert(List<SysMessageJunk> temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "insert" + " param:" + JSON.toJSONString(temp));
		this.saveBatch(temp);
		return temp.size();
	}

	@Override
	public List<SysMessageJunk> listByPage(long userId, long mailId, String column, String word, int offset, int limit,
			int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "listByPage" + " param:" + "mailId:" + mailId
				+ "userId:" + userId);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		LambdaQueryWrapper<SysMessageJunk> wrapper =Wrappers.lambdaQuery(SysMessageJunk.class)
				.eq(SysMessageJunk::getUserId,userId)
				.eq(SysMessageJunk::getMailId,mailId)
				.in(SysMessageJunk::getState,list);
		if (StringUtils.isNotBlank(column) && StringUtils.isNotBlank(word)) {
			column = column.trim();
			word = word.trim();
			if ("subject".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getSubject,word);
			} else if ("bodyText".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getBodyText,word);
			} else if ("mailFrom".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getMailFrom,word);
			} else if ("mailAddress".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getMailAddress,word);
			}
		}
		Page<SysMessageJunk> page = new Page<>();
		page.setCurrent(offset);
		page.setSize(limit);

		return sysMessageJunkMapper.selectList(page, wrapper);
	}

	@Override
	public long count(long userId, long mailId, String column, String word, int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "count" + " param:" + "mailId:" + mailId
				+ "userId:" + userId);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		LambdaQueryWrapper<SysMessageJunk> wrapper =Wrappers.lambdaQuery(SysMessageJunk.class)
				.eq(SysMessageJunk::getUserId,userId)
				.eq(SysMessageJunk::getMailId,mailId)
				.in(SysMessageJunk::getState,list);
		if (StringUtils.isNotBlank(column) && StringUtils.isNotBlank(word)) {
			column = column.trim();
			word = word.trim();
			if ("subject".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getSubject,word);
			} else if ("bodyText".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getBodyText,word);
			} else if ("mailFrom".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getMailFrom,word);
			} else if ("mailAddress".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getMailAddress,word);
			}
		}

		return sysMessageJunkMapper.selectCount(wrapper);
	}

	@Override
	public List<SysMessageJunk> listByPage(long userId, String column, String word, int offset, int limit,
			int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "listByPage" + " param:" + "userId:" + userId);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		LambdaQueryWrapper<SysMessageJunk> wrapper =Wrappers.lambdaQuery(SysMessageJunk.class)
				.eq(SysMessageJunk::getUserId,userId)
				.in(SysMessageJunk::getState,list);
		if (StringUtils.isNotBlank(column) && StringUtils.isNotBlank(word)) {
			column = column.trim();
			word = word.trim();
			if ("subject".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getSubject,word);
			} else if ("bodyText".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getBodyText,word);
			} else if ("mailFrom".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getMailFrom,word);
			} else if ("mailAddress".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getMailAddress,word);
			}
		}
		Page<SysMessageJunk> page = new Page<>();
		page.setCurrent(offset);
		page.setSize(limit);

		return sysMessageJunkMapper.selectList(page, wrapper);
	}

	@Override
	public long count(long userId, String column, String word, int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "count" + " param:" + "userId:" + userId);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		LambdaQueryWrapper<SysMessageJunk> wrapper =Wrappers.lambdaQuery(SysMessageJunk.class)
				.eq(SysMessageJunk::getUserId,userId)
				.in(SysMessageJunk::getState,list);
		if (StringUtils.isNotBlank(column) && StringUtils.isNotBlank(word)) {
			column = column.trim();
			word = word.trim();
			if ("subject".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getSubject,word);
			} else if ("bodyText".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getBodyText,word);
			} else if ("mailFrom".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getMailFrom,word);
			} else if ("mailAddress".equalsIgnoreCase(column)) {
				wrapper.like(SysMessageJunk::getMailAddress,word);
			}
		}

		return sysMessageJunkMapper.selectCount(wrapper);
	}
}
